//
//  CouponListView.m
//  EasyToo
//
//  Created by Jr Lan on 13-8-30.
//
//

#import "CouponListView.h"
#import "UIImageView+OnlineImage.h"

@implementation CouponListView
{
    id<CouponListViewDelegate> _delegate;
}

@synthesize tableview;
@synthesize couponArray;

- (id)initWithFrame:(CGRect)frame andDelegate:(id<CouponListViewDelegate>) mydelegate
{
    self = [super initWithFrame:frame];
    if (self) {
        
        _delegate =  mydelegate;
        
        UITableView* tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        [self addSubview:tableView];
        CGFloat width = self.frame.size.width;
        CGFloat height = self.frame.size.height;
        [tableView setFrame:CGRectMake(0, 0, width, height)];
        [tableView setDelegate:self];
        [tableView setDataSource:self];
        tableView.separatorColor = [UIColor underPageBackgroundColor];
        self.tableview = tableView;
    }
    return self;
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [couponArray count] - 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: nil];
    
    Merchant *couponinfo = [couponArray objectAtIndex:indexPath.row];
    
    //加载图像
    UIImageView *imgview  = [[[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 70, 90)]autorelease];
    [imgview setOnlineImage:couponinfo.imageUrl];
    [cell.contentView addSubview:imgview];
    
    //加载名称
    UILabel *namelbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 10, 200, 40)]autorelease];
    namelbl.text = couponinfo.name;
    namelbl.font = [UIFont systemFontOfSize:15];
    namelbl.textColor = [UIColor redColor];
    namelbl.adjustsFontSizeToFitWidth = YES;
    [cell.contentView addSubview:namelbl];
    
    //加载描述
    UILabel *desclbl = [[[UILabel alloc]initWithFrame:CGRectMake(100, 50, 200, 40)]autorelease];
    desclbl.text = couponinfo.address;
    desclbl.font = [UIFont systemFontOfSize:10];
    desclbl.lineBreakMode = UILineBreakModeWordWrap;
    desclbl.numberOfLines = 0;
    [cell.contentView addSubview:desclbl];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [_delegate detailClick:[couponArray objectAtIndex:indexPath.row]];
}


@end
